

// const URL1 = 'http://118.190.117.227:9000';
const URL1 = 'http://192.168.188.35:9000';

export  class  HTTP {
    static sessionId = 0;
    static userId = 0;
    static master_url = URL1;
    static url = URL1;
    
    static sendRequest(path, data?, handler?, extraUrl?){
        let xhr = cc.loader.getXMLHttpRequest();
        xhr.timeout = 500000000;

        let str = '?';
        if(data){
            for(let k in data){
                if(str != '?'){
                    str += '&';
                }
                str += k + '=' + data[k];
            }
        }
        if(extraUrl == null){
            extraUrl = this.url;
        }
        const requestURL = extraUrl + path + encodeURI(str);
        xhr.open('GET', requestURL, true);
        if(cc.sys.isNative){
            xhr.setRequestHeader('Accept-Encoding', 'gzip,deflate');
            xhr.setRequestHeader('Content-Type', 'text/html;charset=UTF-8');
        }

        xhr.onreadystatechange = () => {
            if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
                console.log('http返回的数据长度是：' + xhr.responseText.length + '。返回的数据是：' + xhr.responseText);
                try {
                    let ret = JSON.parse(xhr.responseText);
                    if(handler !== null){
                        handler(ret);
                    }
                } 
                catch (error) {
                    console.log('解析返回的数据出错：系统错误信息：' + error + '\n看看返回的数据是不是严格的json格式！');
                }
                finally{

                }
            }
        };
        xhr.send();
        return xhr;
    }
}
